// Sizer.cpp: implementation of the CSizer class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Sizer.h"

CSizerCollection::CSizerCollection(HWND hParentWindow)
{
	RECT rect;
	::GetWindowRect(hParentWindow, &rect);
	m_szParentWindow.cx = rect.right - rect.left;
	m_szParentWindow.cy = rect.bottom - rect.top;
}

void CSizerCollection::Add(CSizer* pItem)
	{
		m_vecCollection.push_back(pItem);	
	}

void CSizerCollection::RecalcLayout(int iNewParentWidth, int iNewParentHeight) const
{	
	for (std::vector<CSizer*>::const_iterator ci = m_vecCollection.begin();
		ci != m_vecCollection.end(); ++ci) 
	{
			(*ci)->RecalcLayout(iNewParentWidth, iNewParentHeight);
	}
}
void CSizerCollection::VerifyDragRectangle(WPARAM wParam, LPARAM lParam)
{
	LPRECT lpRect = reinterpret_cast<LPRECT>(lParam);

//check width
	if ( (lpRect->right - lpRect->left) < m_szParentWindow.cx) 
	{
		if ( 
			(wParam == WMSZ_TOPRIGHT) || 
			(wParam == WMSZ_RIGHT) || 
			(wParam == WMSZ_BOTTOMRIGHT) 
			)
				lpRect->right = lpRect->left + m_szParentWindow.cx;
		if ( 
			(wParam == WMSZ_TOPLEFT) || 
			(wParam == WMSZ_LEFT) || 
			(wParam == WMSZ_BOTTOMLEFT) 
		   )
			lpRect->left = lpRect->right - m_szParentWindow.cx;
	}

//check height
	if ( (lpRect->bottom - lpRect->top) <  m_szParentWindow.cy )
	{
		if (
			(wParam == WMSZ_BOTTOMLEFT) ||
			(wParam == WMSZ_BOTTOM) ||
			(wParam == WMSZ_BOTTOMRIGHT)
			)
				lpRect->bottom = lpRect->top + m_szParentWindow.cy;
		if (
			(wParam == WMSZ_TOPLEFT) ||
			(wParam == WMSZ_TOP) ||
			(wParam == WMSZ_TOPRIGHT)
			)	
			lpRect->top = lpRect->bottom - m_szParentWindow.cy;
	}
}


CSizerCollection::~CSizerCollection()
{
	for (std::vector<CSizer*>::const_iterator ci = m_vecCollection.begin();
		ci != m_vecCollection.end(); ++ci) 
	{
			delete (*ci);
	}
}



CSizer::CSizer(HWND hControl, HWND hParentWindow, ResizeMode ResizeMode)
:m_ResizeMode(ResizeMode),
m_hControl(hControl)
{
	_ASSERTE(::IsWindow(m_hControl));	
	_ASSERTE(::IsWindow(hParentWindow));	

	RECT rcControl;
	::GetWindowRect(m_hControl, &rcControl);
//map screen coordz to parent coordz
	::MapWindowPoints(HWND_DESKTOP, hParentWindow,
		(LPPOINT)(&rcControl), sizeof(RECT)/sizeof(POINT));

	m_ptInitialControlTopLeft.x = rcControl.left;
	m_ptInitialControlTopLeft.y = rcControl.top;

	m_szInitialControlSize.cx = rcControl.right - rcControl.left;
	m_szInitialControlSize.cy = rcControl.bottom - rcControl.top;

	RECT rcParentWindow;
	::GetClientRect(hParentWindow, &rcParentWindow);

	m_szInitialParentSize.cx = rcParentWindow.right - rcParentWindow.left;
	m_szInitialParentSize.cy = rcParentWindow.bottom - rcParentWindow.top;
}

CSizer::~CSizer()
{
}

void CSizer::RecalcLayout(int iNewParentWidth, int iNewParentHeight) const
{
//coordz and size for a control

	int x, y, width, height;

	switch (m_ResizeMode)
	{
	case FIXED:
		{
			x = m_ptInitialControlTopLeft.x \
					+ (iNewParentWidth - m_szInitialParentSize.cx);
			y = m_ptInitialControlTopLeft.y \
					+ (iNewParentHeight - m_szInitialParentSize.cy);
			width = m_szInitialControlSize.cx;
			height = m_szInitialControlSize.cy;
			break;
		}

	case HORIZONTAL:
		{
			x = m_ptInitialControlTopLeft.x;
			y = m_ptInitialControlTopLeft.y;
			width = m_szInitialControlSize.cx \
					+ (iNewParentWidth - m_szInitialParentSize.cx);
			height = m_szInitialControlSize.cy;
			break;
		}

	case VERTICAL:
		{
			x = m_ptInitialControlTopLeft.x;
			y = m_ptInitialControlTopLeft.y;
			width = m_szInitialControlSize.cx;
			height = m_szInitialControlSize.cy \
					+ (iNewParentHeight - m_szInitialParentSize.cy);
			break;
		}

	case BOTH:
		{
			x = m_ptInitialControlTopLeft.x;
			y = m_ptInitialControlTopLeft.y;
			width = m_szInitialControlSize.cx \
					+ (iNewParentWidth - m_szInitialParentSize.cx);
			height = m_szInitialControlSize.cy \
					+ (iNewParentHeight - m_szInitialParentSize.cy);
			break;
		}

	default :
		_ASSERTE(false);
	}//~switch (m_ResizeMode)

	::MoveWindow(m_hControl, x, y, width, height, TRUE);  
}
